﻿using System;
using Nemerle.Collections;
using Nemerle.Text;
using Nemerle.Utility;

using SCG = System.Collections.Generic;

namespace Nemerle.Xml
{
  using Node;
  using DocumentElement;

  public module Utils
  {
    public SkipSpaces(this lst : list[Node]) : SCG.IEnumerable[Node]
    {
      lst.FilterLazy(x => !(x is Whitespace))
    }

    public TryForAll2[T1, T2](this lst1 : SCG.IEnumerable[T1], lst2 : SCG.IEnumerable[T2], predicate : T1 * T2 -> bool) : bool
    { //TODO: Плохой код. Может привести к ошибкам. Надо переписать!
      try   { lst1.ForAll2 (lst2, predicate) }
      catch { | _ is ArgumentException => false }
    }

    internal TransformList[T](lst : list[T], transform : ElementBase -> ElementBase) : list[T] where T : ElementBase
    {
        def newNodes = lst.Fold([], fun(x, acc) {
            def res = x.Transform(transform);
            if (res == null) acc else (res :> T) :: acc
          });

        newNodes
    }
  }
}
